<template>
  <div id="app">
    <a-input
      placeholder="请输入任务"
      :value="inputValue"
      class="my_ipt"
      @change="changeInputValue"
    />
    <a-button type="primary" @click="addItemToList">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox
          :checked="item.done"
          @change="
            e => {
              changeChecked(e, item.id);
            }
          "
          >{{ item.info }}</a-checkbox
        >
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{ unDoChange }}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button
            :type="dafaultValue === 'all' ? 'primary' : 'default'"
            @click="changList('all')"
            >全部</a-button
          >
          <a-button
            :type="dafaultValue === 'undone' ? 'primary' : 'default'"
            @click="changList('undone')"
            >未完成</a-button
          >
          <a-button
            :type="dafaultValue === 'done' ? 'primary' : 'default'"
            @click="changList('done')"
            >已完成</a-button
          >
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
export default {
  name: "app",
  data() {
    return {};
  },
  created() {
    this.$store.dispatch("getList");
  },
  computed: {
    ...mapState(["inputValue", "dafaultValue"]),
    ...mapGetters(["unDoChange", "infoList"])
  },
  methods: {
    // 监听文本框的变化
    changeInputValue(e) {
      console.log(e.target.value);
      this.$store.commit("setInputValue", e.target.value);
    },
    // 像列表中新增item项
    addItemToList() {
      if (this.inputValue.trim().length <= 0) {
        return this.$message.error("文本框内容不能为空");
      }
      this.$store.commit("addItems");
    },
    // 实现删除操作
    deleteItem(id) {
      this.$store.commit("delete");
    },
    // 监听复选框的状态变化事件
    changeChecked(e, id) {
      // console.log(e.target.checked);
      // console.log(id);
      const params = {
        id: id,
        status: e.target.checked
      };
      this.$store.commit("changeStatusBox", params);
    },
    // 清除已完成的
    clean() {
      this.$store.commit("clearItem");
    },

    changList(key) {
      console.log(key);
      this.$store.commit("doTag", key);
    }
  }
};
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
